package com.skplanet.rade.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.skplanet.rade.dao.UserTagDAO;
import com.skplanet.rade.domain.UserTag;

@Service(value = "userTagService")
@Transactional
public class UserTagService {

	@Autowired
	private TagService tagService;

	@Autowired
	private UserTagDAO userTagDAO;

	@Autowired
	private PasswordEncoder passwordEncoder;

	
	@Transactional
	public void update(UserTag tag) {
		userTagDAO.update(tag);
	}

	@Transactional
	public void addUserTag(UserTag userTag) {
		userTagDAO.save(userTag);
	}

	@Transactional(readOnly = true)
	public UserTag select(String tagId) {
		UserTag userTag = userTagDAO.select(tagId);
		return userTag;
	}
	
	@Transactional(readOnly = true)
	public boolean keywordExists(String keyword) {
		return userTagDAO.rowExists("keyword", keyword);
	}

	@Transactional
	public void addCount(UserTag userTag)
	{
		userTag.setCount(userTag.getCount()+1);
	}
	
	@Transactional
	public void changeScore(UserTag userTag, double score)
	{
		userTag.setScore(score);
	}
	
//	@Transactional(readOnly = true)
//	public UserTag getUserTagFromKeyword(String keyword, User user) {
//		List<UserTag> utList = userTagDAO.findListByObject("keyword", keyword);
//		Iterator<UserTag> uIter = utList.iterator();
//		UserTag userTag = null;
//		
//		while(uIter.hasNext()) {
//			UserTag temp = uIter.next();
//			if(temp.getUser().getId() == user.getId()) {
//				userTag = temp;
//				break;
//			}
//		}
//		
//		System.out.println(user.getId());
//		
//		if (userTag == null) {
//			userTag = new UserTag();
//			userTag.setKeyword(keyword);
//		}
//		
//		return userTag;
//	}
}